package org.djw.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.djw.basic.util.AjaxResult;
import org.djw.basic.util.PageList;
import org.djw.system.domain.Detail;
import org.djw.system.domain.Type;
import org.djw.system.query.TypeQuery;
import org.djw.system.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 明细类型管理接口
 */
@RestController
@RequestMapping("/dic")
@Api(tags = "明细类型接口",description = "明细类型接口详细描述")
public class TypeController {
    @Autowired
    private ITypeService typeService;
    /**
     * 查询一条数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Type queryById(@PathVariable("id") Long id){
        return typeService.queryById(id);
    }

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping("/list")
    public List<Type> queryAll(){
        return typeService.queryAll();
    }

    /**
     * 分页高级查询
     * @param query
     * @return
     */
    @PostMapping("/page")
    public PageList<Type> queryPage(@RequestBody TypeQuery query){
        return typeService.queryPage(query);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            typeService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }

    /**
     * 新增或者修改
     * @param type
     * @return
     */
    @PostMapping("/save")
    @ApiOperation(value = "明细添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody Type type){
        try {
            if (type.getId() == null){
                typeService.add(type);
            } else {
                typeService.update(type);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,请稍后再试");
        }
    }
}
